package com.sstir.doctor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sstir.doctor.pojo.dto.DoctorDocDTO;
import com.sstir.doctor.pojo.query.DocSearchQuery;
import com.sstir.doctor.pojo.query.GenericQueryPage;
import com.sstir.doctor.pojo.vo.SearchDocVO;
import com.sstir.doctor.result.RespResult;
import com.sstir.doctor.service.EsSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *     搜索-前端控制器
 * </p>
 *
 * @author: fli
 * @email: fli@sstir.cn
 * @date: 2021/1/15 16:21
 */
@RestController
@RequestMapping(value = "/search")
@Api(tags = "ES服务")
@CrossOrigin
public class SearchController {


    @Autowired
    private EsSearchService esSearchService;

    @ApiOperation(value = "分页搜索",response = SearchDocVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", defaultValue = "1",
                    paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页大小", defaultValue = "10",
                    paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "docType", value = "文档类型",
                    paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "keyword", value = "关键词",
                    paramType = "query", dataType = "String")
    })
    @RequestMapping("")
    public RespResult search(
            @RequestParam(required = false,defaultValue = "1") Integer current,
            @RequestParam(required = false,defaultValue = "10") Integer size,
            String docType,
            String keyword) {
        DocSearchQuery docSearchQuery = new DocSearchQuery();
        docSearchQuery.setKeyword(keyword);
        docSearchQuery.setDocType(docType);
        IPage<DoctorDocDTO> dtoPage = esSearchService.searchDocs(new GenericQueryPage<>(current,size,docSearchQuery));
        return  RespResult.ok(
                dtoPage.convert(dto-> {
                    SearchDocVO vo = new SearchDocVO();
                    vo.setId(dto.getId());
                    vo.setDocType(dto.getDocType().getValue());
                    vo.setDocTypeText(dto.getDocType().getText());
                    vo.setContent(dto.getContent());
                    vo.setSourceMap(dto.getSourceMap());
                    return vo;
                })
        );
    }
}
